package com.wy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wy.entity.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 王渝
 */
@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {

    /**
     * 根据用户id 批量查询用户邮箱
     * @param userIds 用户id
     * @return
     */
    @Select("<script>" +
            "select  email" +
            "         FROM user_info" +
            "        WHERE user_id in" +
            "        <foreach collection ='userIds' item='id' separator =',' open = '(' close = ')' >" +
            "           #{id}" +
            "        </foreach >" +
            "</script>" )
    List<String> queryEmail(List<Long> userIds);

    /**
     * 更新用户状态（72小时未付款）
     * @return
     */
    @Update("UPDATE user_info,goods_order set break_count = break_count +1 \n" +
            "where goods_order.`status` = 1 \n" +
            "and timestampdiff(hour,goods_order.create_time,sysdate()) > 72  \n" +
            "and goods_order.buyer_user_id = seller_user_id;")
    int updateBreakCount();

    /**
     * 定时退还保证金
     * @return
     */
    @Update("update user_info,goods_deposit,goods_auction,goods_info\n" +
            "SET balance = balance + goods_deposit.amount \n" +
            "WHERE user_info.user_id = goods_deposit.user_id\n" +
            "and goods_deposit.goods_id = goods_auction.goods_id  \n" +
            "and goods_deposit.goods_id = goods_info.goods_id\n" +
            "and goods_auction.ordering = 2\n" +
            "and goods_deposit.`status` = 1\n" +
            "and goods_info.current_user_id != user_info.user_id\n")
    int updateBalanceTime();

    /**
     * 扣取保证金
     * @param amount 金额
     * @param userId 用户id
     * @return
     */
    @Update("update user_info set balance = balance - #{amount} WHERE user_id = #{userId} and balance - #{amount} >= 0")
    int updateBalance(BigDecimal amount,Long userId);

    /**
     * 退回余额
     * @param amount 金额
     * @param userId 用户id
     * @return
     */
    @Update("update user_info set balance = balance + #{amount} WHERE user_id = #{userId} ")
    int addBalance(BigDecimal amount,Long userId);


    /**
     * 根据订单id查询卖方用户邮箱
     * @param orderId 订单id
     * @return
     */
    @Select(
            "SELECT email FROM user_info,goods_order\n" +
            "WHERE goods_order.buyer_user_id = user_info.user_id\n" +
            "AND order_id =#{userId}\n"
            )
    String queryEmailByOrderId(Long orderId);

    /**
     * 根据商品id查询用户信息
     * @param goodsId 商品id
     * @return
     */
    @Select("SELECT user_name,user_id,head_image,break_count\n" +
            "FROM user_info\n" +
            "WHERE user_id = (SELECT user_id FROM goods_info WHERE goods_id = #{goodsId})")
    UserInfo queryUserInfoByGoodsId(Long goodsId);

    /**
     * 根据用户id查询用户信息
     * @param userId 用户id
     * @return
     */
    @Select("SELECT user_name,user_id,head_image,break_count\n" +
            "FROM user_info\n" +
            "WHERE user_id = #{userId}")
    UserInfo queryUserInfoByUserId(Long userId);
}